<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气查询应用</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .search-box {
            display: flex;
            gap: 10px;
            margin-bottom: 30px;
        }
        .search-input {
            flex: 1;
            padding: 15px;
            border: 2px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        .search-btn {
            padding: 15px 30px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .weather-card {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 20px;
            display: none;
        }
        .weather-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .city-name {
            font-size: 24px;
            font-weight: bold;
        }
        .temperature {
            font-size: 36px;
            color: #667eea;
        }
        .weather-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
        }
        .info-item {
            background: white;
            padding: 15px;
            border-radius: 5px;
            text-align: center;
        }
        .info-label {
            font-size: 12px;
            color: #666;
            margin-bottom: 5px;
        }
        .info-value {
            font-size: 16px;
            font-weight: bold;
        }
        .loading, .error {
            text-align: center;
            padding: 20px;
            display: none;
        }
        .error {
            color: red;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🌤️ 天气查询应用</h1>
            <p>实时天气信息查询</p>
        </div>

        <div class="search-box">
            <input type="text" id="cityInput" class="search-input" placeholder="请输入城市名称" value="北京">
            <button onclick="searchWeather()" class="search-btn">查询天气</button>
        </div>

        <div id="loading" class="loading">正在查询天气信息...</div>
        <div id="error" class="error"></div>

        <div id="weatherCard" class="weather-card">
            <div class="weather-header">
                <div class="city-name" id="cityName">北京</div>
                <div class="temperature" id="temperature">22°C</div>
            </div>
            <div class="weather-info" id="weatherInfo"></div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:5091/api';

        async function searchWeather() {
            const city = document.getElementById('cityInput').value.trim();
            if (!city) {
                showError('请输入城市名称');
                return;
            }

            showLoading();
            hideError();
            hideWeather();

            try {
                const response = await fetch(`${API_BASE}/weather/current/${encodeURIComponent(city)}`);
                const data = await response.json();

                if (data.success) {
                    displayWeather(data.data);
                } else {
                    showError(data.message);
                }
            } catch (error) {
                showError('网络错误，请稍后重试');
            } finally {
                hideLoading();
            }
        }

        function displayWeather(weather) {
            document.getElementById('cityName').textContent = weather.city;
            document.getElementById('temperature').textContent = weather.temperature;
            
            const weatherInfo = document.getElementById('weatherInfo');
            weatherInfo.innerHTML = `
                <div class="info-item">
                    <div class="info-label">天气状况</div>
                    <div class="info-value">${weather.weather}</div>
                </div>
                <div class="info-item">
                    <div class="info-label">湿度</div>
                    <div class="info-value">${weather.humidity}</div>
                </div>
                <div class="info-item">
                    <div class="info-label">风向</div>
                    <div class="info-value">${weather.windDirection}</div>
                </div>
                <div class="info-item">
                    <div class="info-label">风力</div>
                    <div class="info-value">${weather.windPower}</div>
                </div>
                <div class="info-item">
                    <div class="info-label">空气质量</div>
                    <div class="info-value">${weather.airQuality}</div>
                </div>
                <div class="info-item">
                    <div class="info-label">体感温度</div>
                    <div class="info-value">${weather.feelsLike}</div>
                </div>
            `;

            showWeather();
        }

        function showLoading() {
            document.getElementById('loading').style.display = 'block';
        }

        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
        }

        function showError(message) {
            const errorDiv = document.getElementById('error');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        function hideError() {
            document.getElementById('error').style.display = 'none';
        }

        function showWeather() {
            document.getElementById('weatherCard').style.display = 'block';
        }

        function hideWeather() {
            document.getElementById('weatherCard').style.display = 'none';
        }

        // 绑定回车键搜索
        document.getElementById('cityInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                searchWeather();
            }
        });
    </script>
</body>
</html> 